package com.danawa.watcher.front;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.danawa.watcher.front.serviceStrategy.WatcherServiceStrategy;
import com.danawa.watcher.util.StringUtil;
import com.danawa.watcherApi.database.command.common.Operator;
import com.danawa.watcherApi.database.domain.BaseProduct;
import com.danawa.watcherApi.database.domain.InflowProduct;
import com.danawa.watcherApi.database.domain.MemberTracking;
import com.danawa.watcherApi.database.domain.PriceHistory;
import com.danawa.watcherApi.database.service.WatcherService;
import com.danawa.watcherApi.parser.ShopParser;

@Controller 	
@RequestMapping("prod")
public class ProdController {
	
	@Autowired 
	WatcherService watcherService;
	
	@Autowired 
	WatcherServiceStrategy watcherServiceStrategy;
	
	@RequestMapping(value ="/addSiteUrl" , method = RequestMethod.GET)
	public void addSiteUrl(@RequestParam(value = "siteUrl", required = true ) String siteUrl,
			ModelMap modelMap, HttpServletRequest request) throws Throwable{	

		
		Map<String, String> parserData = watcherServiceStrategy.dataParserMap(siteUrl);
		
		int baseProductSeq = 0;
		if(parserData != null){
			String key = parserData.get(ShopParser.PROD_CODE);
			BaseProduct baseProduct = new BaseProduct();
			baseProduct.setOperator(Operator.EQUAL, "key", key);
			List<BaseProduct> baseProducts = watcherService.getBaseProducts(baseProduct);
			
			if(baseProducts.size() > 0){
				baseProduct = baseProducts.get(0);
				baseProductSeq = baseProducts.get(0).getBaseProductSeq();
			}else{
				/**
				 * 기준 상품
				 */
				baseProductSeq = watcherServiceStrategy.setBaseProduct(parserData);
				
				/**
				 *유입상품
				 */
				int inflowProductSeq = 0;
				if(baseProductSeq > 0){
					inflowProductSeq = watcherServiceStrategy.setInflowProduct(baseProductSeq, siteUrl, parserData);
				}
				
				/**
				 * 가격 히스토리
				 */
				if(inflowProductSeq > 0){
					watcherServiceStrategy.setPriceHistory(inflowProductSeq, parserData);
				}
				
				
			}
		}
		modelMap.addAttribute("result", baseProductSeq);
	}
	
	@RequestMapping(value ="/prodView" , method = RequestMethod.GET)
	public String prodView(@RequestParam(value = "baseProductSeq", required = true ) int baseProductSeq,
			@RequestParam(value = "month", required = false, defaultValue = "12" ) int month,
			@RequestParam(value = "siteTypeIndex", required = false, defaultValue = "0" ) int siteTypeIndex,
			ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws Throwable{	
		
		BaseProduct baseProduct = watcherService.getBaseProductWithInflowProducts(baseProductSeq);
		InflowProduct inflowProduct= new InflowProduct();
		String url = "";
		
		if(baseProduct != null){
			inflowProduct = baseProduct.getInflowProducts().get(siteTypeIndex);
			url = inflowProduct.getLink();
		}
		
		Map<String, String> parserData = watcherServiceStrategy.dataParserMap(url);

		if(parserData != null){
			boolean isProd = false;
			/**
			 * 가격
			 */
			int maximume		= inflowProduct.getMaximumPrice();	//최고가
			int minimum 		= inflowProduct.getMinimumPrice();	//최저가
			int present 		= inflowProduct.getPresentPrice();		//현재가
			int parserPresent = StringUtil.toInt(parserData.get(ShopParser.PROD_PRICE));		// 파싱 현재가
			
			//최고가
			if(maximume < parserPresent){
				inflowProduct.setMaximumPrice(parserPresent);
				isProd = true;
			}
			//최저가
			if(minimum > parserPresent){
				inflowProduct.setMinimumPrice(parserPresent);
				isProd = true;
			}
			//현재가
			if(present != parserPresent){
				inflowProduct.setPresentPrice(parserPresent);
				isProd = true;
			}
			
			//가격변동시 업데이트
			if(isProd){
				inflowProduct = baseProduct.getInflowProducts().get(siteTypeIndex);
				inflowProduct.setUpdateDateTime(new Date());
				//가격 변동시 추후 update
				//watcherService.setInflowProduct(inflowProduct);
				
				PriceHistory priceHistory = new PriceHistory();
				priceHistory.setInflowProductSeq(inflowProduct.getInflowProductSeq());
				priceHistory.setPrice(parserPresent);
				priceHistory.setInputDateTime(new Date());
				watcherService.setPriceHistory(priceHistory);
			}
		}
		
		MemberTracking memberTracking = new MemberTracking();
		int memberSeq = 0; //회원번호
		memberTracking.setOperator(Operator.EQUAL, "memberSeq", memberSeq);
		memberTracking.setOperator(Operator.EQUAL, "inflowProductSeq", inflowProduct.getInflowProductSeq());
		List<MemberTracking> memberTrackings = watcherService.getMemberTrackings(memberTracking);
		
		
		modelMap.addAttribute("inflowProduct", baseProduct.getInflowProducts().get(siteTypeIndex));
		modelMap.addAttribute("baseProduct", baseProduct);
		return "prod/prodView";
		
	}
	
}
